#include "traywidget.h"
#include "ui_traywidget.h"


TrayWidget::TrayWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TrayWidget)
{
    ui->setupUi(this);

    Tray_Type = 0;
    Drop_Type = 0;
    DripNum = 0;

    Rows_of_TrayType_1=10;
    Columns_of_TrayType_1=10;

    Rows_of_TrayType_2=5;
    Columns_of_TrayType_2=5;

    Rows_of_TrayType_3=5;
    Columns_of_TrayType_3=5;
}

TrayWidget::~TrayWidget()
{
    delete ui;
}

void TrayWidget::paintEvent(QPaintEvent *)
{
    Drawtray();
}

void TrayWidget::SetTrayType(int type)
{
    Tray_Type = type;
    update();
}

void TrayWidget::SetDripNum(int num)
{
    DripNum = num;
    update();
}
void TrayWidget::SetDropType(int Type)
{
    Drop_Type = Type;
    update();
}

void TrayWidget::Drawtray()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,  true);
    painter.setPen(QPen(QColor(140,140,140),4,Qt::SolidLine));
    painter.setBrush(QColor(140,140,140));

    int DripNumCount = 0;

    //30mm板型
    if(Tray_Type == 1)
    {
        for (int j = 0;j < Rows_of_TrayType_1;j++) {
            for (int i = 0;i < Columns_of_TrayType_1;i++) {
                painter.drawEllipse(10 + 65 * i ,10 + 65 * j ,55 ,55);
                if(DripNumCount<DripNum)
                {
                   painter.setBrush(QColor(255,0,0));
                   painter.drawEllipse(13 + 65 * i ,13 + 65 * j ,49 ,49);
                   painter.save();
                   painter.setPen(Qt::black);
                   DrawDrip(20 + 65 * i ,30 + 65 * j );
                   painter.restore();
                   painter.setBrush(QColor(140,140,140));
                   DripNumCount++;
                }
            }

        }
    }

    //60mm板型
    else if(Tray_Type == 2)
    {
        for (int j = 0;j < Rows_of_TrayType_2;j++) {
            for (int i = 0;i < Columns_of_TrayType_2;i++) {
                painter.drawEllipse(10 + 120 * i ,10 + 120 * j ,110 ,110);
                if(DripNumCount<DripNum)
                {
                   painter.setBrush(QColor(255,0,0));
                   painter.drawEllipse(20 + 120 * i ,20 + 120 * j ,90 ,90);
                   painter.save();
                   painter.setPen(Qt::black);
                   DrawDrip(65 + 120 * i ,65 + 120 * j );
                   painter.restore();
                   painter.setBrush(QColor(140,140,140));
                   DripNumCount++;
                }
            }

        }
    }
    //方板型
    else if(Tray_Type == 3)
    {
        for (int j = 0;j < Rows_of_TrayType_3;j++) {
            for (int i = 0;i < Columns_of_TrayType_3;i++) {
                painter.drawRect(10 + 90 * i ,10 + 90 * j ,80 ,80);
                if(DripNumCount<DripNum)
                {
                   painter.setBrush(QColor(255,0,0));
                   painter.drawRect(20 + 90 * i ,20 + 90 * j ,60 ,60);
                   painter.save();
                   painter.setPen(Qt::black);
                   DrawDrip(50 + 90 * i ,50 + 90 * j );
                   painter.restore();
                   painter.setBrush(QColor(140,140,140));
                   DripNumCount++;
                }
            }

        }
    }
}

void TrayWidget::DrawDrip(int x, int y)
{
    QPainter painter2(this);
    painter2.setPen(QPen(QColor(0,255,0),4,Qt::SolidLine));
    painter2.setBrush(QColor(0,255,0));
    switch(Drop_Type)
    {
        case 0:
            break;
        case 1:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"1");
            break;
        case 2:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"2");
            break;
        case 3:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"3");
            break;


    }
}

